package com.yh.common.exception;

import com.yh.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理器
 * Created by skh on 2017/12/22
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

	/**
	 * 处理全局异常
	 *
	 * @param e
	 * @return
	 */
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public R handleException(Exception e) {
		log.error(e.getMessage(), e);
		return R.error(e.getMessage());
	}

	/**
	 * 处理运行时异常
	 *
	 * @param e
	 * @return
	 */
	@ExceptionHandler(RuntimeException.class)
	@ResponseBody
	public R handleRuntimeException(RuntimeException e) {
		log.error(e.getMessage(), e);
		return R.error(e.getMessage());
	}

	/**
	 * 处理自定义异常
	 *
	 */
	@ExceptionHandler(BizException.class)
	@ResponseBody
	public R handleBizException(BizException e) {
		log.error(e.getMessage(), e);
		return R.error(e.getCode(), e.getMessage());
	}

	/**
	 * 处理自定义异常
	 *
	 */
	@ExceptionHandler(RRException.class)
	@ResponseBody
	public R handleRRException(RRException e) {
		log.error(e.getMessage(), e);
		return R.error(e.getCode(), e.getMessage());
	}


}